package com.aba.cjms.student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

import com.aba.cjms.pojo.ConnectionDatabaseForMySQLUseJdbc;

public class StudentDao {
	
	
	static  Connection connection;
static PreparedStatement pstmt;
static ResultSet rset;
	
//设置属性 用来保留 登录时候的学生账号 和 密码  设置全局变量 ；
static String student_zhanghao;
static String student_password;
//自助注册

public static void register() throws Exception{
   Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();

   PreparedStatement pstmt = connection.prepareStatement("insert into student(saccount,sname,spassword,teacherid) values(?,?,?,?)");

   System.out.println("请输入要注册的账号号码");
   Scanner scanner = new Scanner(System.in);
   String accout = scanner.next();
   pstmt.setString(1,accout);
   System.out.println("请输入密码");
   String password = scanner.next();
   pstmt.setString(2,password);
   System.out.println("请输入姓名");
   String name = scanner.next();
   pstmt.setString(3,name);
   System.out.println("请输入老师id");
   int teacher = scanner.nextInt();
   pstmt.setInt(4,teacher);

   pstmt.executeUpdate();
    if (pstmt.executeUpdate() > 0)
    {
        System.out.println("注册成功");
        System.out.println("请重新登录-----");
        login();
    }
    else
    {
        System.out.println("注册失败");

    }
   connection.close();
   pstmt.close();
   return;
    
}
   
//查询学生自己的成绩

public static void queryScore() throws Exception{
    Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
    PreparedStatement pstmt = connection.prepareStatement("select * from student where saccount = ?");
    pstmt.setString(1,student_zhanghao);
    ResultSet rset = pstmt.executeQuery();
    while (rset.next())
    {
        System.out.println("查询成功！！");
        System.out.println("编号："+rset.getInt(1));
        System.out.println("姓名："+rset.getString(2));
        System.out.println("账号："+rset.getString(3));
        System.out.println("密码："+rset.getString(4));
        
        System.out.println("成绩："+rset.getInt(5));
        System.out.println("老师id："+rset.getInt(6));
        System.out.println("-----------------");
        showMenu();
    }
}



//登录 根据学号和密码 跟据学号在数据库中查找密码，如果密码正确，则登录成功，否则登录失败。这是一个登陆的方法
public static void login() throws Exception{
    Scanner scanner = new Scanner(System.in);
    
    System.out.println("请输入学号进行学生端登录：");
    String xuehao = scanner.next();
    System.out.println("请输入密码你的密码：");
    String mima = scanner.next();
    try{
    	Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
        //根据学号在数据库中查找密码 与输入的密码进行比较
        PreparedStatement pstmt = connection.prepareStatement("select * from student where saccount = ? and spassword = ?");
        pstmt.setString(1,xuehao);
        pstmt.setString(2,mima);
        ResultSet rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("登录成功！");
            
          //将正确的账号和密码保存在全局变量 student_password和student_zhanghao中
            student_password = rs.getString("spassword");
            student_zhanghao = rs.getString("saccount");
            showMenu();
        }else{
            System.out.println("登录失败！");
            System.out.println("请重新登录");
            login();
        }
        rs.close();
        pstmt.close();
        return;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


//选择登录或注册
			public static void choice(){ 
				System.out.println("1.登录");
				System.out.println("2.注册");
						try{
							Scanner scanner = new Scanner(System.in);
									int choice = scanner.nextInt();
									switch(choice){
									case 1: login();
            
									break; 
									case 2: register();
									break;
									default: System.out.println("输入错误");
													}
							}catch(Exception e){
									System.out.println("输入错误");
											}
										}
				//登录后的菜单
				public static void showMenu() throws Exception{
					System.out.println("查询成绩：1");

					System.out.println("返回登录: 2");

					Scanner  sc = new Scanner(System.in);
					int choice = sc.nextInt();
					switch(choice){
					case 1: queryScore();
					break;
					case 2: login();
					break;

					}
				}

//    public static void main(String[] args) {
//    	choice();
//	}

}
